package bairui.international.console.web.controller.user.offset;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.querydsl.core.types.Predicate;

import bairui.international.model.domain.jpa.TB_ElectronicTicket;
import bairui.international.model.domain.jpa.TB_ElectronicTicketOrder;
import bairui.international.model.status.State_ElectronicTicket;
import bairui.international.model.type.Type_ElectronicTicket;
import bairui.international.service.Service_ElectronicTicket;

/**
 *
 * 分配电子券
 *
 * @generated
 * 
 * @see STest_C_User_Offset_AssignOffset_Page 测试
 *
 */
@Controller
public class C_User_Offset_AssignOffset_Page
 extends SC_User_Offset_AssignOffset_Page 
{
	
	@Autowired private Service_ElectronicTicket service_ElectronicTicket;
	
	/**
	 * 分页列表
	 */
	@SuppressWarnings("serial")
	@Action(Page.class)
	@GetMapping(value=Page.RequestMappingValue, consumes=MediaType.APPLICATION_JSON_VALUE)
	public @FomatOutput @ResponseBody Object page(
		HttpServletRequest request, 
		@PathVariable(name=Page.PathVariableName.UserId) long userId
	) throws Exception {
		org.springframework.data.domain.Page<TB_ElectronicTicket> page = service_ElectronicTicket.page(
				it->Page.Filter.query(it, request, new Page.Filter.FilterExtends() {
					@Override public Predicate gainType(Type_ElectronicTicket type) {
						return it.electronicTicketOrder.type.eq(type);
					}
					@Override public Predicate gainOneuseFlag(Boolean oneuseFlag) {
						return it.electronicTicketOrder.oneuseFlag.eq(oneuseFlag);
					}
					@Override public Predicate gainName(String name) {
						return it.electronicTicketOrder.name.like("%" + name + "%");
					}
					@Override public Predicate gainGiveFlag(Boolean giveFlag) {
						return it.electronicTicketOrder.giveFlag.eq(giveFlag);
					}
					@Override public Predicate gainExpiryDate_start(Date expiryDate) {
						return it.electronicTicketOrder.expiryDate.gt(expiryDate);
					}
					@Override public Predicate gainExpiryDate_end(Date expiryDate) {
						return it.electronicTicketOrder.expiryDate.lt(expiryDate);
					}
				})
					.and(it.assignedUser.id.ne(userId).or(it.assignedUser.isNull()))
					.and(it.takor.id.ne(userId).or(it.takor.isNull()))
					.and(it.state.eq(State_ElectronicTicket.Generated)),
				Page.Output.page(request), 
				it->Page.Output.sort(it, request, new Page.Output.OrderExtends() {
					@Override public Sort gainType(Direction direction) {
						return sort(direction, it.electronicTicketOrder.type);
					}
					@Override public Sort gainPrice(Direction direction) {
						return sort(direction, it.electronicTicketOrder.price);
					}
					@Override public Sort gainOneuseFlag(Direction direction) {
						return sort(direction, it.electronicTicketOrder.oneuseFlag);
					}
					@Override public Sort gainName(Direction direction) {
						return sort(direction, it.electronicTicketOrder.name);
					}
					@Override public Sort gainGiveFlag(Direction direction) {
						return sort(direction, it.electronicTicketOrder.giveFlag);
					}
					@Override public Sort gainExpiryDate(Direction direction) {
						return sort(direction, it.electronicTicketOrder.expiryDate);
					}
				})
			);
		//
		return Page.Output.output(
				page
					.map(it->new Page.Output.Model(it){
							private TB_ElectronicTicketOrder order = null;
							@Override protected void assign_before() {
								order = it.getElectronicTicketOrder();
							}
							@Override public String assignName() {
								return order.getName();
							}
							@Override public Double assignPrice() {
								return order.getPrice();
							}
							@Override public Type_ElectronicTicket assignType() {
								return order.getType();
							}
							@Override public Boolean assignGiveFlag() {
								return order.getGiveFlag();
							}
							@Override public Boolean assignOneuseFlag() {
								return order.getOneuseFlag();
							}
							@Override public java.util.Date assignExpiryDate() {
								return order.getExpiryDate();
							}
						}
					)
			);
	}
	

}
